package xiaochangsama.the_age_of_immortals.procedures;

import xiaochangsama.the_age_of_immortals.TheAgeOfImmortalsModVariables;
import xiaochangsama.the_age_of_immortals.TheAgeOfImmortalsMod;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;

import net.minecraft.world.World;
import net.minecraft.world.IWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.Entity;

import java.util.Map;
import java.util.HashMap;

public class FirstJoinInProcedure {
	@Mod.EventBusSubscriber
	private static class GlobalTrigger {
		@SubscribeEvent
		public static void onEntityJoin(EntityJoinWorldEvent event) {
			World world = event.getWorld();
			Entity entity = event.getEntity();
			double i = entity.getPosX();
			double j = entity.getPosY();
			double k = entity.getPosZ();
			Map<String, Object> dependencies = new HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			executeProcedure(dependencies);
		}
	}
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				TheAgeOfImmortalsMod.LOGGER.warn("Failed to load dependency entity for procedure FirstJoinIn!");
			return;
		}
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				TheAgeOfImmortalsMod.LOGGER.warn("Failed to load dependency world for procedure FirstJoinIn!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		IWorld world = (IWorld) dependencies.get("world");
		if (((((entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new TheAgeOfImmortalsModVariables.PlayerVariables())).firstTimeJoinIn) == (true)) && (entity instanceof PlayerEntity))) {
			{
				boolean _setval = (boolean) (false);
				entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.firstTimeJoinIn = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
			{
				double _setval = (double) TheAgeOfImmortalsModVariables.MapVariables.get(world).worldPlayerID;
				entity.getCapability(TheAgeOfImmortalsModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.playerID = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
			TheAgeOfImmortalsModVariables.MapVariables
					.get(world).worldPlayerID = (double) (TheAgeOfImmortalsModVariables.MapVariables.get(world).worldPlayerID + 1);
			TheAgeOfImmortalsModVariables.MapVariables.get(world).syncData(world);
		}
	}
}
